//
//  WBNavigationController.swift
//  swift微博
//
//  Created by qlwl on 17/2/25.
//  Copyright © 2017年 qlwl. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 隐藏默认的 NavigationBar
        navigationBar.isHidden = true
    }
    
    //重写push方法
    //所有的push方法，都会调用该方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        //如果不是栈低控制器，跟控制器不需要处理
        //隐藏底部的 tabBar
        if childViewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            
            // 判断控制器的类型
            if let vc = viewController as? WBBaseViewController {
                var title = "返回"
                if childViewControllers.count == 1 {
                    // title 显示首页的标题，只有一个自控制器时显示栈顶标题
                    title = childViewControllers.first?.title ?? "返回"
                }
                
                // 取出自定义的 navItem
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, targert: self, action: #selector(popToParent), isBack: true)
            }

        }
        
        super.pushViewController(viewController, animated: animated)
    }
    
    @objc fileprivate func popToParent() {
        popViewController(animated: true)
    }

}
